//书店店员有一张链表形式的书单，每个节点代表一本书，节点中的值表示书的编号。为更方便整理书架，店员需要将书单倒过来排列，就可以从最后一本书开始整理，逐一将书放
//回到书架上。请倒序返回这个书单链表。 
//
// 
//
// 示例 1： 
//
// 
//输入：head = [3,6,4,1]
//
//输出：[1,4,6,3]
// 
//
// 
//
// 提示： 
//
// 0 <= 链表长度 <= 10000 
//
// Related Topics 栈 递归 链表 双指针 👍 487 👎 0


package LeetCode.editor.cn;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;

/**
 * @author ldltd
 * @date 2025-04-22 23:48:26
 * @description LCR 123.图书整理 I
 */
public class CongWeiDaoTouDaYinLianBiaoLcof{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 CongWeiDaoTouDaYinLianBiaoLcof fun=new CongWeiDaoTouDaYinLianBiaoLcof();
	 	 Solution solution = fun.new Solution();

	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)

 // Definition for singly-linked list.
  public class ListNode {
      int val;
      ListNode next;
     ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
class Solution {
    public int[] reverseBookList1(ListNode head) {
		Deque<Integer> st=new ArrayDeque<>();
		while (head!=null) {
			st.push(head.val);
			head=head.next;
		}
		int [] res=new int[st.size()];
		int inx=0;
		while (!st.isEmpty()){
			res[inx++]=st.pop();
		}
		return res;
    }

	ArrayList<Integer> tmp = new ArrayList<Integer>();
	public int[] reverseBookList(ListNode head) {
		recur(head);
		int[] res = new int[tmp.size()];
		for(int i = 0; i < res.length; i++)
			res[i] = tmp.get(i);
		return res;
	}
	void recur(ListNode head) {
		if(head == null) return;
		recur(head.next);
		tmp.add(head.val);
	}
}
//leetcode submit region end(Prohibit modification and deletion)

}
